package com.ihr360.commons.resolver;

import com.ihr360.commons.constant.Constants;
import com.ihr360.commons.entity.AuthUserInfo;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


/**
 * @author tom
 */
@Component
public class AuthUserResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return AuthUserInfo.class.equals(parameter.getParameterType());
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {

        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

        String companyId = request.getHeader(Constants.Session.COMPANY_ID);
        if (StringUtils.isEmpty(companyId)) {
            companyId = request.getParameter(Constants.Session.COMPANY_ID);
        }

        String companyName = request.getHeader(Constants.Session.COMPANY_NAME);
        if (StringUtils.isEmpty(companyName)) {
            companyName = request.getParameter(Constants.Session.COMPANY_NAME);
        } else {
            byte[] bytes = Base64Utils.decodeFromString(companyName);
            companyName = new String(bytes, StandardCharsets.UTF_8);
        }
        String userId = request.getHeader(Constants.Session.USER_ID);
        if (StringUtils.isEmpty(userId)) {
            userId = request.getParameter(Constants.Session.USER_ID);
        }
        String userName = request.getHeader(Constants.Session.USER_NAME);
        if (StringUtils.isEmpty(userName)) {
            userName = request.getParameter(Constants.Session.USER_NAME);
        } else {
            byte[] bytes = Base64Utils.decodeFromString(userName);
            userName = new String(bytes, StandardCharsets.UTF_8);
        }
        String staffId = request.getHeader(Constants.Session.STAFF_ID);
        if (StringUtils.isEmpty(staffId)) {
            staffId = request.getParameter(Constants.Session.STAFF_ID);
        }
        String mobileNo = request.getHeader(Constants.Session.MOBILE_NO);
        if (StringUtils.isEmpty(mobileNo)) {
            mobileNo = request.getParameter(Constants.Session.MOBILE_NO);
        }

        String sessionId = request.getHeader(Constants.Session.SESSION_ID);
        if (StringUtils.isEmpty(mobileNo)) {
            sessionId = request.getParameter(Constants.Session.SESSION_ID);
        }

        if (!StringUtils.isEmpty(companyId)) {
            return new AuthUserInfo(companyId, companyName, userId, userName, mobileNo, staffId, sessionId);
        }
        return null;
    }

}
